package com.zhuangfei.hos_timetableview.ability;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnSlideBuildAdapter;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

import java.util.List;

/**
 * 简单展示侧边栏的样式效果（默认的侧边栏简单配置）
 */
public class SlideAbility extends Ability {

    TimetableView mTimetableView;
    Button moreButton;
    String[] popupMenus;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_slide);
        // 添加表头的include
        StackLayout stInclude = (StackLayout) findComponentById(ResourceTable.Id_st_include);
        stInclude.addComponent(LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_header_simple, stInclude, false));
        moreButton = (Button) findComponentById(ResourceTable.Id_id_more);
        moreButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showPopmenu();
            }
        });
        initTimetableView();
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .showView();
    }

    /**
     * 显示弹出菜单
     */
    public void showPopmenu() {
        if (popupMenus == null) {
            popupMenus = new String[]{"显示节次时间", "隐藏节次时间", "修改侧边栏背景", "修改节次文本颜色", "修改时间文本颜色", "自定义侧边栏", "取消自定义侧边栏"};
        }
        new XPopup.Builder(getContext())
                .hasShadowBg(false)
                .atView(moreButton)
                .asAttachList(popupMenus, null,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                switch (text) {
                                    case "显示节次时间":
                                        showTime();
                                        break;
                                    case "隐藏节次时间":
                                        hideTime();
                                        break;
                                    case "修改侧边栏背景":
                                        modifySlideBgColor(Color.YELLOW.getValue());
                                        break;
                                    case "修改节次文本颜色":
                                        modifyItemTextColor(Color.RED.getValue());
                                        break;
                                    case "修改时间文本颜色":
                                        modifyItemTimeColor(Color.RED.getValue());
                                        break;
                                    case "自定义侧边栏":
                                        customSlideView();
                                        break;
                                    case "取消自定义侧边栏":
                                        cancelCustomSlideView();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }, 0, 0).show();
    }

    /**
     * 显示时间
     * 设置侧边栏构建监听，TimeSlideAdapter是控件实现的可显示时间的侧边栏
     * 只修改了侧边栏的属性，所以只更新侧边栏即可（性能高），没有必要更新全部（性能低）
     *
     * @see OnSlideBuildAdapter
     */
    protected void showTime() {
        String[] times = new String[]{
                "8:00", "9:00", "10:10", "11:00",
                "15:00", "16:00", "17:00", "18:00",
                "19:30", "20:30", "21:30", "22:30"
        };
        OnSlideBuildAdapter slideAdapter = new OnSlideBuildAdapter();
        slideAdapter.setTimes(times);
        mTimetableView.callback(slideAdapter);
        mTimetableView.updateSlideView();
    }

    /**
     * 隐藏时间
     * 将侧边栏监听置Null后，会默认使用默认的构建方法，即不显示时间
     * 只修改了侧边栏的属性，所以只更新侧边栏即可（性能高），没有必要更新全部（性能低）
     */
    protected void hideTime() {
        mTimetableView.callback((ISchedule.OnSlideBuildListener) null)
                .updateSlideView();
    }

    /**
     * 修改侧边栏背景,默认的使用的是OnSlideBuildAdapter，
     * 所以可以强转类型
     *
     * @param color 颜色值，rgb
     */
    protected void modifySlideBgColor(int color) {
        OnSlideBuildAdapter listener = (OnSlideBuildAdapter) mTimetableView.onSlideBuildListener();
        listener.setBackground(color);
        mTimetableView.updateSlideView();
    }

    /**
     * 修改侧边栏节次文本的颜色值
     *
     * @param color 侧边栏节次文本的颜色值，rgb
     */
    protected void modifyItemTextColor(int color) {
        OnSlideBuildAdapter listener = (OnSlideBuildAdapter) mTimetableView.onSlideBuildListener();
        listener.setTextColor(color);
        mTimetableView.updateSlideView();
    }

    /**
     * 修改侧边栏时间文本的颜色值
     *
     * @param color 侧边栏时间文本的颜色值，rgb
     */
    protected void modifyItemTimeColor(int color) {
        String[] times = new String[]{
                "8:00", "9:00", "10:10", "11:00",
                "15:00", "16:00", "17:00", "18:00",
                "19:30", "20:30", "21:30", "22:30"
        };
        OnSlideBuildAdapter listener = (OnSlideBuildAdapter) mTimetableView.onSlideBuildListener();
        listener.setTimes(times)
                .setTimeTextColor(color);
        mTimetableView.updateSlideView();
    }

    /**
     * 自定义侧边栏效果
     * 使用自定义的布局文件实现的文字居顶部的效果（默认居中）
     */
    protected void customSlideView() {
        mTimetableView.callback(
                new OnSlideBuildAdapter() {
                    @Override
                    public Component getView(int pos, LayoutScatter inflater, int itemHeight, int marTop) {
                        // 获取View并返回，注意设置marTop值
                        Component component = inflater.parse(ResourceTable.Layout_item_custom_slide, null, false);
                        Text tv = (Text) component.findComponentById(ResourceTable.Id_item_slide_textview);
                        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, itemHeight);
                        lp.setMargins(0, marTop, 0, 0);
                        tv.setLayoutConfig(lp);
                        tv.setText((pos + 1) + "");
                        return component;
                    }
                })
                .updateSlideView();
    }

    /**
     * 取消自定义的侧边栏，回到默认状态
     * 只需要将监听器置空即可
     */
    protected void cancelCustomSlideView() {
        mTimetableView.callback((ISchedule.OnSlideBuildListener) null)
                .updateSlideView();
    }
}
